package cn.baizhi.service.impl;

import cn.baizhi.entity.*;
import cn.baizhi.feign.CityClient;
import cn.baizhi.feign.ClazzClient;
import cn.baizhi.feign.GroupClient;
import cn.baizhi.feign.TagClient;
import cn.baizhi.mapper.StudentMapper;
import cn.baizhi.mapper.StudentTagMapper;
import cn.baizhi.service.StudentService;
import cn.baizhi.util.DateUtil;
import cn.baizhi.vo.StudentDTO;
import cn.baizhi.vo.StudentDTO1;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LZM
 * @since 2021-12-03
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentTagMapper studentTagMapper;

    @Autowired
    private ClazzClient clazzClient;

    @Autowired
    private CityClient cityClient;

    @Autowired
    private GroupClient groupClient;

    @Autowired
    private TagClient tagClient;

    @Override
    public List<StudentDTO> queryAll() {
        // 查全部学生
        List<Student> students = studentMapper.selectList(null);

        List<StudentDTO> studentDTOS = new ArrayList<>();

        // 遍历所有学生
        for (Student student : students ) {
            StudentDTO studentDTO = new StudentDTO();
            BeanUtils.copyProperties(student,studentDTO);

//          调用客户端查询
            Clazz clazz = clazzClient.selectOne(student.getClazzId()).getBody();
            City city = cityClient.selectOne(student.getCityId()).getBody();
            Group group = groupClient.selectOne(student.getGroupId()).getBody();

            QueryWrapper<StudentTag> studentTagQueryWrapper = new QueryWrapper<>();
            //根据学生id查找对象的标签id
            QueryWrapper<StudentTag> tagQueryWrapper = studentTagQueryWrapper.eq("sid", student.getId());
            List<StudentTag> tagList = studentTagMapper.selectList(tagQueryWrapper);

            List<Integer> listInteger = new ArrayList<>();
            for (StudentTag studentTag : tagList) {
                listInteger.add(studentTag.getTid());
            }

            List<Tag> tags = new ArrayList<>();
            // 根据查找到的标签ID查询对应的标签对象
            for (Integer integer : listInteger) {
                Tag tag = tagClient.selectOne(integer).getBody();
                tags.add(tag);
            }

            studentDTO.setCity(city);
            studentDTO.setClazz(clazz);
            studentDTO.setGroup(group);
            studentDTO.setTags(tags);

            studentDTOS.add(studentDTO);
        }

        return studentDTOS;

    }

    @Override
    public StudentDTO add(StudentDTO1 studentDTO1) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO1,student);

        student.setAge(DateUtil.getAge(studentDTO1.getBir()));
        student.setStarts(DateUtil.getConstellation(studentDTO1.getBir()));// 星座
        student.setAttr(DateUtil.getYear(studentDTO1.getBir())); //生肖

        studentMapper.insert(student);

        List<Integer> tagIds = studentDTO1.getTagIds();
        for (Integer tagId : tagIds) {
            studentTagMapper.insert(new StudentTag(student.getId(),tagId));
        }

        StudentDTO studentDTO = new StudentDTO();
        BeanUtils.copyProperties(student,studentDTO);

        Clazz clazz = clazzClient.selectOne(student.getClazzId()).getBody();
        City city = cityClient.selectOne(student.getCityId()).getBody();
        Group group = groupClient.selectOne(student.getGroupId()).getBody();

        QueryWrapper<StudentTag> studentTagQueryWrapper = new QueryWrapper<>();
        //根据学生id查找对象的标签id
        QueryWrapper<StudentTag> tagQueryWrapper = studentTagQueryWrapper.eq("sid", student.getId());
        List<StudentTag> tagList = studentTagMapper.selectList(tagQueryWrapper);

        List<Integer> listInteger = new ArrayList<>();
        for (StudentTag studentTag : tagList) {
            listInteger.add(studentTag.getTid());
        }

        List<Tag> tags = new ArrayList<>();
        // 根据查找到的标签ID查询对应的标签对象
        for (Integer integer : listInteger) {
            Tag tag = tagClient.selectOne(integer).getBody();
            tags.add(tag);
        }

        studentDTO.setCity(city);
        studentDTO.setClazz(clazz);
        studentDTO.setGroup(group);
        studentDTO.setTags(tags);


        return studentDTO;
    }
}
